
package com.ysf.cn.springbootshrio.util;
import com.ysf.cn.springbootshrio.pojo.Permission;
import com.ysf.cn.springbootshrio.pojo.User;
import com.ysf.cn.springbootshrio.service.PermissionService;
import com.ysf.cn.springbootshrio.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author ysf
 * @date 2020/2/13 12:17
 */

public class UserRealm extends AuthorizingRealm {

    private static final Logger LOGGER= LoggerFactory.getLogger(UserRealm.class);

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;


    /**
     * 授权
     * @param principalCollection
     * @return
     */

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.err.println("走授权方法了");
        //拿到当前登陆的对象
       /* Subject subject= SecurityUtils.getSubject();
        User users=(User) subject.getPrincipal();
        System.out.println(users);*/
        //从principalCollection中获取用户登陆的信息
        Object principal=principalCollection.getPrimaryPrincipal();
        System.out.println("principal 的值为是："+principal.toString());
        //利用用户的登陆信息来作为当前用户的角色或权限(可能查询数据库)
        //User user=(User) principalCollection.getPrimaryPrincipal();
        User user=userService.finduserById(principal.toString());
        //根据用户id查询当前用户下的权限信息
        List<Permission> permissionlist=permissionService.selectPermissionByUserId(user.getUserid());
        Set<String> roles=new HashSet<>();
        for(Permission permission:permissionlist){
            roles.add(permission.getPermission());
        }
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        //添加权限
        //info.addStringPermission("update");
        //info.addStringPermission("systemadd");
        info.addStringPermissions(roles);
        //LOGGER.info("doGetAuthenticationInfo");
        System.out.println(info);
        return info;
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("走认证方法");
        UsernamePasswordToken token=(UsernamePasswordToken)authenticationToken;
        //String pwd=new String((char[]) token.getCredentials());
        //通过token中的 username调用 finduserById(username)方法从数据库中查询数据
        User user=userService.finduserById(token.getUsername());
        if(user==null){
            System.out.println("账号或密码不正确");
            throw new UnknownAccountException("账号或密码不正确");
        }
        //LOGGER.info("doGetAuthenticationInfo");   ByteSource.Util.bytes(user.getSalt())
        ///SimpleAuthorizationInfo infos=new SimpleAuthenticationInfo(user,user.getPassword().toCharArray(), ByteSource.Util.bytes(user.getSalt()),getName());
        SimpleAuthenticationInfo infos = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),ByteSource.Util.bytes(user.getSalt()), getName());
        System.out.println("info is=>"+infos);
        return infos;
    }
}

